package com.zhouchao.web;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhouchao.bean.Dorm;
import com.zhouchao.bean.DormManager;
import com.zhouchao.bean.Student;
import com.zhouchao.dao.DormBuildDao;
import com.zhouchao.dao.DormDao;
import com.zhouchao.dao.StudentDao;
import com.zhouchao.util.DbUtil;

public class Change extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		DormDao dormDao = new DormDao();
		StudentDao studao=new StudentDao();
		DbUtil dbUtil = new DbUtil();
		String dormName=request.getParameter("dormName");
		HttpSession session = request.getSession();
		List<Student> stuList =null;
		
		String action = request.getParameter("action");
		Connection con = null;
		DormManager manager = (DormManager)(session.getAttribute("currentUser"));
		int buildId = manager.getDormBuildId();
		
		if("login".equals(action)){
		try {
			con=dbUtil.getCon();
				String buildName = DormBuildDao.dormBuildName(con, buildId);
				List<Dorm> dormlist = dormDao.dormList(con, buildId);
			
				request.setAttribute("dormBuildName", buildName);
				request.setAttribute("dormlist", dormlist);
				
				
				request.setAttribute("mainPage", "dormManager/change.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		}else if("change".equals(action)){
			String left=request.getParameter("left");
			String right=request.getParameter("right");
			String vv=request.getParameter("v");
			String v=new String(vv.getBytes("ISO-8859-1"),"utf-8");
			String vv1=request.getParameter("v1");
			String v1=new String(vv1.getBytes("ISO-8859-1"),"utf-8");
			String[] stu=v.split(",");
			String[] stu1=v1.split(",");
			for(int i=0;i<stu.length;i++){
				
				//System.out.println(stu[i]);
				try {
					con=dbUtil.getCon();
					studao.studentUpdate1(con, stu[i],left);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			for(int i=0;i<stu1.length;i++){
				
				try {
					con=dbUtil.getCon();
					studao.studentUpdate1(con, stu1[i],right);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			System.out.println(left);
			System.out.println(v);
			System.out.println(right);
			System.out.println(v1);
			request.getRequestDispatcher("change?action=login").forward(request, response);
			
		}
		else{
			
			
			 response.setContentType("text/xml");
		        response.setHeader("Cache-Control","no-cache");
		       
		       dormName=request.getParameter("id").toString();
		        String xml_start="<selects>";
		        String xml_end="</selects>";
		        String xml="";
			
			try {
				con=dbUtil.getCon();
				stuList=studao.studentList(con, dormName,buildId);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		    
		        
		   for(int i=0;i<stuList.size();i++){
			   xml+="<select><value>"+stuList.get(i).getStuNumber();
				 xml+="</value><text>"+stuList.get(i).getStuNumber()+"  "+stuList.get(i).getName()+"</text></select>";
					
					
				System.out.println(stuList.get(i).getName());	
				
				}
		        
		        String last_xml=xml_start+xml+xml_end;
		        response.getWriter().write(last_xml);
			
		}
		
		
		
	}



	

}
